// pages/home-music/index.js
import {
  bannerapi,
  getmenu
} from '../../service/api-music'
import {rankingStore , rankingMap} from "../../store/ranking-store"
Page({

  data: {
    banner: [],
    swiperhigth: 0,
    tracks:[],
    menulist:[],
    rankings:{0:{},2:{},3:{}}
  },
  searchclick: function () {
    // console.log("1111")
    wx.navigateTo({
      url: '../detil-search/index',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.bannerme()
    this.remen()
    rankingStore.dispatch('gettankings')
    rankingStore.onState('hotrinking',(res=>{
      // console.log(res)
      if(!res.tracks) return
      const tracks = res.tracks.slice(0,6)
      this.setData({tracks})
    }))
    rankingStore.onState('newRanking',this.gehtnewRankinghandlet(0))
    rankingStore.onState('originRanking',this.gehtnewRankinghandlet(2))
    rankingStore.onState('upRanking',this.gehtnewRankinghandlet(3))
  },
  // 获取banner图片
  bannerme: function () {
    bannerapi().then(res => {
      // console.log(res.banners)
      this.setData({
        banner: res.banners
      })
    })
  },
  // 获取热门歌单
  remen(){
    getmenu().then(res=>{
      // console.log(res)
      this.setData({menulist:res.playlists})
    })
  },
  // 获取img图片高度
  swiperimageheigth: function () {
    const query = wx.createSelectorQuery()
    query.select('.swiper-image').boundingClientRect()
    query.exec((res) =>{
      // console.log()
      this.setData({swiperhigth:res[0].height})
    })
  },
  gehtnewRankinghandlet:function (idx){
    // console.log(res)
    return (res)=>{
      // console.log(res)
      if(Object.keys(res).length==0) return
    const name = res.name
    const coverImage = res.coverImgUrl
    const songList =res.tracks.slice(0,3)
    const playCount =res.playCount
    const rankingobj = {name,coverImage,songList,playCount}
    const newRankings = {...this.data.rankings,[idx]:rankingobj}
    this.setData({rankings:newRankings})
    }
    // console.log(this.data.rankings)
  },
  // 监听歌曲点击事件
  headetap:function(e){
    var id = e.currentTarget.dataset.idx
    console.log(id);
    this.navto(id)
  },
  // 跳转到歌单列表页面
  navto:function (idx) {
    const rankmapname = rankingMap[idx]
    wx.navigateTo({
      url: `/pages/detill-songs/index?ranking=${rankmapname}`,
    })
  },

})